package com.wocai.jrt.orgadmin.stats.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.GenericAction;
import com.vteba.web.editer.StringEditor;
import com.wocai.jrt.orgadmin.appointment.model.Appointment;
import com.wocai.jrt.orgadmin.stats.bean.PerFormReqBean;
import com.wocai.jrt.orgadmin.stats.service.spi.PerformanceStatsService;
import com.wocai.jrt.orgadmin.utils.UserUtils;


/**
 * 日期统计控制器
 * @author zhangyz
 * @date 2015年9月22日09:19:19
 */
@Controller
@RequestMapping("/performStats")
public class PerformanceStatsAction extends GenericAction<Appointment>{
	private static final Logger LOGGER = LoggerFactory.getLogger(PerformanceStatsAction.class);
	@Inject
	private PerformanceStatsService performanceStatsServiceImpl;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		// 空字符串转换为null
		binder.registerCustomEditor(String.class, new StringEditor());
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(
				"yyyy-MM-dd"), true));
	}

	
	/**
     * 跳转至业绩按日统计
     * @param model 参数
     * @return 预约单List
     */
    @RequestMapping("/performDay")
    public String performDay(PerFormReqBean model, Map<String, Object> maps) {
        return "stats/performStats/performDay";
    }
    
    /**
     * 测试缓存
     */
    @RequestMapping("/cache")
    public void testCache(){
    	//计算业绩统计数据
    	performanceStatsServiceImpl.preformanceStatsCount();
    }
    
    /**
     * 测试缓存扩展
     */
    @RequestMapping("/cacheEx")
    public void testCacheEx(){
    	//计算当天、当月、当年业绩统计数据
    	performanceStatsServiceImpl.updateCurPerformanceStatsRedis();
    }
    
    /**
	 * 获取业绩统计数据
	 * @param model 参数
	 * @return 预约单List
	 */
	@ResponseBody
	@RequestMapping("/queryData")
	public Map<String, Object> queryDay(@RequestBody PerFormReqBean model) {
		Map<String, Object> result = new HashMap<String, Object>();
    	try {
    		Date date = model.getEndTime();
    		String formatStr = model.getFormatStr();
			if (date != null) {
				model.setEndTime(new DateTime(date).plusHours(23).plusMinutes(59).plusSeconds(59)
						.toDate());
			}
			
			if(formatStr == null){
				LOGGER.info("performStats data query param is not completely");
				return null;
			}
			model.setOrgId(UserUtils.getOrgId());
			Map<String, Object> map =performanceStatsServiceImpl.getDemandCache(model);
			result.put("data", map);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("performStats data query error, msg=[{}]", e.getMessage());
		}
        return result;
	}
	
    /**
     * 跳转至业绩按月统计
     * @param model 参数
     * @return 预约单List
     */
    @RequestMapping("/performMonth")
    public String performMonth(PerFormReqBean model, Map<String, Object> maps) {
        return "stats/performStats/performMonth";
    }
    /**
     * 跳转至业绩按年统计
     * @param model 参数
     * @return 预约单List
     */
    @RequestMapping("/performYear")
    public String performYear(PerFormReqBean model, Map<String, Object> maps) {
        return "stats/performStats/performYear";
    }
}
